package com.ocean.dynamic;

/**
 * 判断是否是子序列
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2023/03/14 <br>
 */
public class IsSubsequence {
    public static void main(String[] args) {
        IsSubsequence isSubsequence = new IsSubsequence();
        String s = "abc", t = "ahbgdc";
        boolean res = isSubsequence.isSubsequence(s, t);
        System.out.println(res);

        s = "axc";
        t = "ahbgdc";

        res = isSubsequence.isSubsequence(s, t);
        System.out.println(res);

        s = "axc";
        t = "ax";
        res = isSubsequence.isSubsequence(s, t);
        System.out.println(res);
    }

    public boolean isSubsequence(String s, String t) {
        int n = s.length();
        int m = t.length();
        int j = 0;
        int i = 0;
        while (i < n && j < m) {
            if (t.charAt(j) == s.charAt(i)) {
                i++;
            }
            j++;
        }
        return i == n;
    }
}
